package com.burgeon.backend.SHOP.controller;

import com.burgeon.backend.SHOP.entity.PayData;
import com.burgeon.backend.SHOP.entity.presell.EarnestVO;
import com.burgeon.backend.SHOP.entity.presell.Presell;
import com.burgeon.backend.SHOP.mappers.GoodsMapper;
import com.burgeon.backend.SHOP.service.AppPresellService;
import com.burgeon.backend.SHOP.service.ex.OrderExceptionApp;
import com.burgeon.backend.SHOPBACKEND.rest.GetGoodsCode;
import com.burgeon.backend.entity.Info;
import com.burgeon.backend.mappers.AccountMapper;
import com.burgeon.backend.stsutil.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("shop/presell")
@RestController
public class AppPresellController extends BaseController {

    @Autowired
    private AppPresellService appPresellService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private AccountMapper accountMapper;

    @RequestMapping("getimg")
    public ResponseResult<String> getGroupImg(HttpSession session) {
        String token = session.getAttribute("token").toString();
        String data = appPresellService.getImg(token);

        return new ResponseResult<String>(SUCCESS,data);
    }

    //支付尾款
    @RequestMapping("payTheBalance")
    public ResponseResult<PayData> payTheBalance(String pid,String mid, String aid, HttpSession session) {
        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        PayData data = appPresellService.payForThebalance(pid, aid, mid, phone, uid, openid, token);

        return new ResponseResult<PayData>(SUCCESS,data);
    }

    @RequestMapping("OrderConfigData")
    public ResponseResult<EarnestVO> OrderConfigData(String id) {
        EarnestVO data = appPresellService.OrderConfigData(id);
        return new ResponseResult<EarnestVO>(SUCCESS,data);
    }

    @RequestMapping("EarnestOrNot")
    public ResponseResult<String> EarnestOrNot(HttpSession session) {

        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        List<EarnestVO> earnestVOS = appPresellService.presellOrderData(uid, token);
        String data = "N";
        if (earnestVOS.size()>0){
            data = "Y";
        }

        return new ResponseResult<String>(SUCCESS,data);
    }

    @RequestMapping("test")
    public ResponseResult<List<EarnestVO>> test() {

        String token = "lfwd";
        String uid = "2272";
        List<EarnestVO> data = appPresellService.presellOrderData(uid, token);

        return new ResponseResult<List<EarnestVO>>(SUCCESS,data);
    }

    @RequestMapping("presellOrderData")
    public ResponseResult<List<EarnestVO>> presellOrderData(HttpSession session) {

        String token = session.getAttribute("token").toString();
        String uid = session.getAttribute("uid").toString();
        List<EarnestVO> data = appPresellService.presellOrderData(uid, token);

        return new ResponseResult<List<EarnestVO>>(SUCCESS,data);
    }

    /**
     * 定金
     *
     * @param gid
     * @param code
     * @param color
     * @param size
     * @param comment
     * @param session
     * @return
     */
    @RequestMapping("earnest")
    public ResponseResult<PayData> earnest(String gid,String pid, String code, String color,String emp,
                                           String size, @RequestParam(value = "comment", required = false, defaultValue = " ") String comment,
                                           HttpSession session) {

        if (null == session.getAttribute("uid")) {
            return new ResponseResult<PayData>(NOTLOG);
        }

        String uid = session.getAttribute("uid").toString();
        String token = session.getAttribute("token").toString();
        String openid = session.getAttribute("app_openid").toString();
        String phone = session.getAttribute("phone").toString();

        String barcode = goodsMapper.getSizeCodeByCodeColorSize(code, color, size);

        if (null == barcode) {
            Info info = accountMapper.GetTokenData(token);
            GetGoodsCode getGoodsCode = new GetGoodsCode(info.getUrl(), info.getAccount(), info.getPassword());
            try {
                code = getGoodsCode.getData(code, color, size);
            } catch (Exception e) {
                e.printStackTrace();
                throw new OrderExceptionApp("出现错误");
            }
        } else {
            code = barcode;
        }

        PayData data = appPresellService.createPresellEarnest(uid, openid,pid,gid, code,
                color + "-" + size, phone, token, comment, emp);
        return new ResponseResult<PayData>(SUCCESS, data);
    }


    @RequestMapping("result")
    public ResponseResult<Void> findEarnestResult(String id) {

        appPresellService.findEarnestResult(id);

        return new ResponseResult<>(SUCCESS);
    }


    @RequestMapping("agreementData")
    public ResponseResult<String> agreementData(HttpSession session) {

        String token = session.getAttribute("token").toString();

        String data = appPresellService.agreementData(token);

        return new ResponseResult<String>(SUCCESS, data);
    }


    @RequestMapping("data")
    public ResponseResult<List<Presell>> Data(HttpSession session) {

        String token = session.getAttribute("token").toString();
        List<Presell> data = appPresellService.getData(token);

        return new ResponseResult<List<Presell>>(SUCCESS, data);
    }

}
